//
//  DBJLoginViewController.m
//  DBJ
//
//  Created by 花化华 on 2016/11/14.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "DBJLoginViewController.h"
#import "DBJRegistViewController.h"
#import "NSString+Regular.h"
#import "SVProgressHUD.h"
#import "DBJUserDefaultsTool.h"
#import "SSKeychain.h"
#import "DBJLoginModel.h"

@interface DBJLoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;

@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;

@end

@implementation DBJLoginViewController

- (void)dbjSetupSubviews{
//    [super dbjSetupSubviews];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
//    [SVProgressHUD dismiss];
}

#pragma mark - 按钮点击事件
/**
 * 登录
 */
- (IBAction)loginClick:(id)sender {

    NSString *name = _phoneTextField.text;
    NSString *password = _passwordTextField.text;
    
    //判断用户是否存在
    if ([DBJUserDefaultsTool objetForKey:name]) {
        
        //判断密码是否正确
        if ([[SSKeychain passwordForService:BUNDLEID account:name] isEqualToString:password]){
            
            //存储／更新当前帐号
            [DBJUserDefaultsTool saveObject:name forKey:CURRENTID];
            
            [DBJTools dbjShowSVTitle:@"正在登录中" andTime:60];
            
            //自动登录
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                //发送通知、DBJLeftBarController接收
                [[NSNotificationCenter defaultCenter] postNotificationName:LOGINSUCCESS object:nil userInfo:nil];
            });
            
        }else{
            [DBJTools dbjShowSVErrorTitle:@"密码输入错误" andTime:STANTIME];
            return;
        }
        
        //将对应的model打印
//        NSData *modelData = [DBJUserDefaultsTool objetForKey:[NSString stringWithFormat:@"%@-MODEL",name]];
//        DBJLoginModel *model = [NSKeyedUnarchiver unarchiveObjectWithData:modelData];
//        NSLog(@"DBJLoginModel: %@",model);
        
    }else{
        [DBJTools dbjShowSVErrorTitle:@"账号输入错误" andTime:STANTIME];
        return;
    }
    
}

/**
 * 短信登录
 */
- (IBAction)messageLoginClick:(id)sender {
    NSLog(@"messageLoginClick");
}

/**
 * 注册新账号
 */
- (IBAction)registClick:(id)sender {
//    DBJRegistViewController *registViewController = [[DBJRegistViewController alloc]init];
//    registViewController.title = @"注册";
//    [self.navigationController pushViewController:registViewController animated:YES];
}


/**
 * 忘记密码
 */
- (IBAction)foundPaswdClick:(id)sender {
    NSLog(@"foundPaswdClick");
}


@end
